package org.ielts.action.security;

import static com.yifeng.struts2.ResultConstants.*;
import static com.yifeng.struts2.ResultParameters.setJson;
import static com.yifeng.struts2.ActionUtils.*;
import static com.yifeng.struts2.RequestParameters.*;

import org.ielts.action.BaseAction;
import org.ielts.domain.base.Department;
import org.ielts.service.security.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;

import com.yifeng.modules.annotations.Module;
import com.yifeng.modules.annotations.Operation;
import com.yifeng.utils.JsonResultBuilder;

@Module(namespaceName = "security")
public class DepartmentAction extends BaseAction {

	@Autowired
	DepartmentService departmentService;

	@Operation(result = FILL)
	public String listDepartment() {

		Iterable<Department> departments = departmentService.findAll();

		put("departments", departments);

		return FILL;
	}

	@Operation(result = JSON)
	public String doCreateDepartment() {
		String name = form.readString("name");

		departmentService.createDepartment(name);

		setJson(JsonResultBuilder.success(true).msg("提交成功").json());

		return JSON;
	}

	@Operation(result = JSON)
	public String doModifyDepartment() {

		String id = form.readString("id");
		String name = form.readString("name");

		departmentService.modifyDepartment(id, name);

		setJson(JsonResultBuilder.success(true).msg("提交成功").json());
		return JSON;
	}

	@Operation(result = JSON)
	public String doRemoveDepartment() {
		String id = form.readString("id");

		departmentService.removeDepartment(id);

		setJson(JsonResultBuilder.success(true).msg("提交成功").json());
		return JSON;
	}
}
